package org.ala.tiktools_clickhouse_generator.generator.mapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.ala.tiktools_clickhouse_generator.config.ConfigInfo;
import org.ala.tiktools_clickhouse_generator.generator.AbstractCreator;
import org.ala.tiktools_clickhouse_generator.generator.dao.DaoInfo;
import org.ala.tiktools_clickhouse_generator.generator.entity.EntityInfo;
import org.ala.tiktools_clickhouse_generator.table.TableInfo;
import org.ala.tiktools_clickhouse_generator.tools.FileTools;

import freemarker.template.TemplateException;

/**
 *
 *	@author ala
 *	@date 2025年6月9日
 */
public class MapperCreator extends AbstractCreator {

	
	public MapperInfo create(ConfigInfo c, TableInfo ti, EntityInfo ei, DaoInfo di) throws IOException, TemplateException {
		Map<String, Object> dataMeta = new HashMap<>();
        MapperInfo mi = new MapperInfo(c.getPart().getOut().getMapper(), ti, c.getPart().getAuthor(), ei, di);
        dataMeta.put("m", mi);
        
        String out = c.getPart().getOut().getMapper().getPkg();
        out = this.getOutRootPath() + out.replaceAll("\\.", "/");
        FileTools.existsAndMakeDirs(out);
       
        String mapperOut = out + "/" + mi.getClassName() + ".xml";
        output("Mapper.ftl", dataMeta, mapperOut);
        
        String mapperAppenderOut = out + "/" + mi.getClassName() + "Appender.xml";
        output("MapperAppend.ftl", dataMeta, mapperAppenderOut);

        System.out.println("生成Mapper类. path:" + out);
        return mi;
	}
}
